package com.lh.tams.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.lh.tams.controller.qo.CaseQo;
import com.lh.tams.controller.vo.CaseAllDetail;
import com.lh.tams.controller.vo.CaseVo;
import com.lh.tams.controller.vo.ConfirmVo;
import com.lh.tams.dao.CaseDao;
import com.lh.tams.dao.CloseDao;
import com.lh.tams.dao.CommonDao;
import com.lh.tams.dao.ConfirmDao;
import com.lh.tams.dao.DealDao;
import com.lh.tams.dao.IdentifyDao;
import com.lh.tams.dao.PartyDao;
import com.lh.tams.dao.RecordDao;
import com.lh.tams.dao.RegisterDao;
import com.lh.tams.dao.SimpleDao;
import com.lh.tams.dao.system.PersonDao;
import com.lh.tams.domain.TamsCase;
import com.lh.tams.domain.TamsClose;
import com.lh.tams.domain.TamsCommon;
import com.lh.tams.domain.TamsConfirm;
import com.lh.tams.domain.TamsDeal;
import com.lh.tams.domain.TamsIdentify;
import com.lh.tams.domain.TamsPerson;
import com.lh.tams.domain.TamsRecord;
import com.lh.tams.domain.TamsRegister;
import com.lh.tams.domain.TamsSimple;
import com.lh.tams.exception.BusinessException;
import com.lh.tams.util.DataGridModel;
import com.lh.tams.util.DateUtil;
import com.lh.tams.util.Global;
import com.lh.tams.util.ID;
import com.lh.tams.util.Page;
import com.lh.tams.util.Status;

@Service
public class CaseService {
	private CaseDao caseDao;
	private CommonDao commonDao;
	private RegisterDao registerDao;
	private IdentifyDao identifyDao;
	private ConfirmDao confirmDao;
	private DealDao dealDao;
	private CloseDao closeDao;
	private RecordDao recordDao;
	private SimpleDao simpleDao;
	private PersonDao personDao;
	private PartyDao partyDao;
		
	public void setPartyDao(PartyDao partyDao) {
		this.partyDao = partyDao;
	}

	public void setCloseDao(CloseDao closeDao) {
		this.closeDao = closeDao;
	}

	public void setRecordDao(RecordDao recordDao) {
		this.recordDao = recordDao;
	}

	public void setDealDao(DealDao dealDao) {
		this.dealDao = dealDao;
	}

	public void setIdentifyDao(IdentifyDao identifyDao) {
		this.identifyDao = identifyDao;
	}
	
	public void setRegisterDao(RegisterDao registerDao) {
		this.registerDao = registerDao;
	}

	public void setCaseDao(CaseDao caseDao) {
		this.caseDao = caseDao;
	}
	
	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}

	public void setSimpleDao(SimpleDao simpleDao) {
		this.simpleDao = simpleDao;
	}

	public void setConfirmDao(ConfirmDao confirmDao) {
		this.confirmDao = confirmDao;
	}
	
	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	/**
	 * 根据案件编号查询案件
	 * @param caseNo
	 * @return
	 */
	public TamsCase query(String caseNo){
		Page page = caseDao.query(caseNo);
		List list = page.getRows();
		if(list.size()>0){
			return (TamsCase) list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * 根据案件编号查询一般案件
	 * @param caseNo
	 * @return
	 */
	public TamsCommon queryCommon(String caseNo){
		Page page = commonDao.query(caseNo);
		List list = page.getRows();
		if(list.size()>0){
			return (TamsCommon) list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * 根据caseQo查询案件
	 * @param dgm
	 * @param caseQo
	 * @return
	 */
	public Page queryForCase(DataGridModel dgm,CaseQo caseQo){
		return caseDao.queryForCase(dgm, caseQo);
	}
	
	/**
	 * 根据caseQo统计案件
	 * @param dgm
	 * @param caseQo
	 * @return
	 */
	public Page queryForCount(DataGridModel dgm,CaseQo caseQo){
		return caseDao.queryForCount(dgm, caseQo);
	}
	
	/**
	 * 审批模块caseQo查询案件
	 * @param dgm
	 * @param caseQo
	 * @return
	 */
	public Page queryForCheck(DataGridModel dgm,CaseQo caseQo){
		return caseDao.queryForCheck(dgm, caseQo);
	}
	
	/**
	 * 分配模块caseQo查询案件
	 * @param dgm
	 * @param caseQo
	 * @return
	 */
	public Page queryForOut(DataGridModel dgm,CaseQo caseQo){
		return caseDao.queryForOut(dgm, caseQo);
	}
	
	/**
	 * 案件查询模块中    根据caseQo查询案件
	 * @param dgm
	 * @param caseQo
	 * @return
	 */
	public Page caseControl(DataGridModel dgm,CaseQo caseQo,String flag){
		Page page = caseDao.queryCase(dgm, caseQo);
		List<TamsCase> cases = page.getRows();
		
		//存放caseVo
		List<CaseVo> list = new ArrayList<CaseVo>();
		for(int i=0;i<cases.size();i++){
			cases.get(i).getTamsRegisters();
			
			CaseVo vo = new CaseVo();
			TamsCase c = cases.get(i);
			//案件基本信息写入
			vo.setCaseNo(c.getCaseNo());
			vo.setAlarmName(c.getAlarmName());
			vo.setAlarmPhone(c.getAlarmPhone());
			vo.setCaseAddress(c.getCaseAddress());
			vo.setAlarmDate(c.getAlarmDate());
			vo.setDealType(c.getDealType());
			vo.setCheckType(c.getCheckType());
			vo.setIsDistribute(c.getDistributeStatus());
			
			
			//计算操作和剩余天数
			TamsRegister register= registerDao.queryByCase(c.getCaseNo());
			TamsIdentify identify = identifyDao.getCaseIdentifByNo(c.getCaseNo());
			TamsDeal deal=dealDao.getCaseDealByNo(c.getCaseNo());
			TamsConfirm confirm=confirmDao.getCaseConfirmByNo(c.getCaseNo());
			TamsRecord record=recordDao.getCaseRecordByNo(c.getCaseNo());
			
			//根据案件是一般还是简易进行分别处理
			if(c.getDealType()==Status.CASE_DEAL_COMMON){
				//按照是否操作了立案来处理
				if(register!=null && register.getStartDate()!=null && register.getRegisterDate()==null){
					long day=DateUtil.days(register.getStartDate(),new Date());
					vo.setRegisterLeftDate(Global.COMMON_REGISTER-day);
				}else if(register!=null&&register.getStartDate()!=null&&register.getRegisterDate()!=null){
					long day=DateUtil.days(register.getStartDate(),register.getRegisterDate());
					vo.setRegisterDate(day-Global.COMMON_REGISTER);
				}
				
				//按照是否操作了鉴定来处理
				if(identify!=null && identify.getStartDate()!=null && identify.getEndDate()==null){
					long day=DateUtil.days(identify.getStartDate(),new Date());
					vo.setIdentifyLeftDate(Global.COMMON_IDENTIFY-day);
				}else if(identify!=null&&identify.getStartDate()!=null&&identify.getEndDate()!=null){
					long day=DateUtil.days(identify.getStartDate(),identify.getEndDate());
					vo.setIdentifyDate(day-Global.COMMON_IDENTIFY);
				}
				
				//按照是否操作了认定来处理
				if(confirm!=null&&confirm.getStartDate()!=null&&confirm.getConfirmDate()==null){
					long day=DateUtil.days(confirm.getStartDate(), new Date());
					vo.setConfirmLeftDate(Global.COMMON_CONFIRM-day);
				}else if(confirm!=null&&confirm.getStartDate()!=null&&confirm.getConfirmDate()!=null){
					long day=DateUtil.days(confirm.getStartDate(), confirm.getConfirmDate());
					vo.setConfirmDate(day-Global.COMMON_CONFIRM);
				}
				
				//按照是否操作了调解来处理
				if(deal!=null&&deal.getBeginDate()!=null&&deal.getEndDate()==null){
					long day=DateUtil.days(deal.getBeginDate(), new Date());
					vo.setDealLeftDate(Global.COMMON_DEAL-day);
				}else if(deal!=null&&deal.getBeginDate()!=null&&deal.getEndDate()!=null){
					long day=DateUtil.days(deal.getBeginDate(), deal.getEndDate());
					vo.setDealDate(day-Global.COMMON_DEAL);
				}
				
				//按照是否操作了归档来处理
				if(record!=null&&record.getStartDate()!=null&&record.getRecordDate()==null){
					long day=DateUtil.days(record.getStartDate(), new Date());
					vo.setRecordLeftDate(Global.COMMON_RECORD-day);
				}else if(record!=null&&record.getStartDate()!=null&&record.getRecordDate()!=null){
					long day=DateUtil.days(record.getStartDate(), record.getRecordDate());
					vo.setRecordDate(day-Global.COMMON_RECORD);
				}
				
			}else if(c.getDealType()==Status.CASE_DEAL_SIMPLE){
				//按照是否操作了认定来处理
				if(confirm!=null&&confirm.getStartDate()!=null&&confirm.getConfirmDate()==null){
					long day=DateUtil.days(confirm.getStartDate(), new Date());
					vo.setConfirmLeftDate(Global.SIMPLE_CONFIRM-day);
				}else if(confirm!=null&&confirm.getStartDate()!=null&&confirm.getConfirmDate()!=null){
					long day=DateUtil.days(confirm.getStartDate(), confirm.getConfirmDate());
					vo.setConfirmDate(day-Global.SIMPLE_CONFIRM);
				}
				
				//按照是否操作了调解来处理
				if(deal!=null&&deal.getBeginDate()!=null&&deal.getEndDate()==null){
					long day=DateUtil.days(deal.getBeginDate(), new Date());
					vo.setDealLeftDate(Global.SIMPLE_DEAL-day);
				}else if(deal!=null&&deal.getBeginDate()!=null&&deal.getEndDate()!=null){
					long day=DateUtil.days(deal.getBeginDate(), deal.getEndDate());
					vo.setDealDate(day-Global.SIMPLE_DEAL);
				}
				
				//按照是否操作了归档来处理
				if(record!=null&&record.getStartDate()!=null&&record.getRecordDate()==null){
					long day=DateUtil.days(record.getStartDate(), new Date());
					vo.setRecordLeftDate(Global.SIMPLE_RECORD-day);
				}else if(record!=null&&record.getStartDate()!=null&&record.getRecordDate()!=null){
					long day=DateUtil.days(record.getStartDate(), record.getRecordDate());
					vo.setRecordDate(day-Global.COMMON_RECORD);
				}
			}
			
			
			//案件进度写入
			if(c.getDealType()==Status.CASE_DEAL_COMMON){
				TamsCommon con = commonDao.getCaseCommonByNo(c.getCaseNo());
				if("deal".equals(flag)){
					if(con.getIsConfirm()!=Status.IS_FINISH_YES || con.getIsRecord()==Status.IS_FINISH_YES || con.getIsDeal()==Status.IS_FINISH_YES){
						continue;
					}
				}
				if(con!=null){
					vo.setIsRegister(con.getIsRegister());
					vo.setIsIdentify(con.getIsIdentify());
					vo.setIsConfirm(con.getIsConfirm());
					vo.setIsDeal(con.getIsDeal());
					vo.setIsClose(con.getIsClose());
					vo.setIsRecord(con.getIsRecord());
				}
				
			}else if(c.getDealType()==Status.CASE_DEAL_SIMPLE){
				TamsSimple si = simpleDao.getCaseSimpleByNo(c.getCaseNo());
				if("deal".equals(flag)){
					if(si.getIsConfirm()!=Status.IS_FINISH_YES || si.getIsRecord()==Status.IS_FINISH_YES || si.getIsDeal()==Status.IS_FINISH_YES){
						continue;
					}
				}
				if(si!=null){
					vo.setIsConfirm(si.getIsConfirm());
					vo.setIsDeal(si.getIsDeal());
					vo.setIsRecord(si.getIsRecord());
				}
			}else if(c.getDealType()==Status.CASE_DEAL_NOPLACE || c.getDealType()==Status.CASE_DEAL_CONSLUT 
					|| c.getDealType()==Status.CASE_DEAL_QUICK|| c.getDealType()==Status.CASE_DEAL_GOAWAY){
				TamsRecord r = recordDao.getCaseRecordByNo(c.getCaseNo());
				if(r!=null){
					vo.setIsRecord(r.getIsFinish());
				}
			}
			list.add(vo);
		}
		//放入page对象中list数据
		page.setRows(list);
		return page;
	}
	
	public Page queryCase(DataGridModel dgm,CaseQo caseQo,String flag){
		Page page = caseDao.queryCase(dgm, caseQo);
		List<TamsCase> cases = page.getRows();
		//存放caseVo
		List<CaseVo> list = new ArrayList<CaseVo>();
		for(int i=0;i<cases.size();i++){
			CaseVo vo = new CaseVo();
			TamsCase c = cases.get(i);
			vo.setCaseNo(c.getCaseNo());
			vo.setAlarmName(c.getAlarmName());
			vo.setAlarmPhone(c.getAlarmPhone());
			vo.setCaseAddress(c.getCaseAddress());
			vo.setAlarmDate(c.getAlarmDate());
			vo.setDealType(c.getDealType());
			vo.setCheckType(c.getCheckType());
			vo.setIsDistribute(c.getDistributeStatus());
			if(c.getDealType()==Status.CASE_DEAL_COMMON){
				TamsCommon con = commonDao.getCaseCommonByNo(c.getCaseNo());
				if("deal".equals(flag)){
					if(con.getIsConfirm()!=Status.IS_FINISH_YES || con.getIsRecord()==Status.IS_FINISH_YES || con.getIsDeal()==Status.IS_FINISH_YES){
						continue;
					}
				}
				if(con!=null){
					vo.setIsRegister(con.getIsRegister());
					vo.setIsIdentify(con.getIsIdentify());
					vo.setIsConfirm(con.getIsConfirm());
					vo.setIsDeal(con.getIsDeal());
					vo.setIsClose(con.getIsClose());
					vo.setIsRecord(con.getIsRecord());
				}
				
			}else if(c.getDealType()==Status.CASE_DEAL_SIMPLE){
				TamsSimple si = simpleDao.getCaseSimpleByNo(c.getCaseNo());
				if("deal".equals(flag)){
					if(si.getIsConfirm()!=Status.IS_FINISH_YES || si.getIsRecord()==Status.IS_FINISH_YES || si.getIsDeal()==Status.IS_FINISH_YES){
						continue;
					}
				}
				if(si!=null){
					vo.setIsConfirm(si.getIsConfirm());
					vo.setIsDeal(si.getIsDeal());
					vo.setIsRecord(si.getIsRecord());
				}
			}else if(c.getDealType()==Status.CASE_DEAL_NOPLACE || c.getDealType()==Status.CASE_DEAL_CONSLUT 
					|| c.getDealType()==Status.CASE_DEAL_QUICK|| c.getDealType()==Status.CASE_DEAL_GOAWAY){
				TamsRecord r = recordDao.getCaseRecordByNo(c.getCaseNo());
				if(r!=null){
					vo.setIsRecord(r.getIsFinish());
				}
			}
			list.add(vo);
		}
		//放入page对象中list数据
		page.setRows(list);
		return page;
	}
	
	/**
	 * 立案列表查询
	 * @param dgm
	 * @param caseQo
	 * @param personId
	 * @return
	 */
	public Page queryRegister(DataGridModel dgm,CaseQo caseQo,long personId){
		Page page = registerDao.query(dgm, caseQo,personId);
		List list = page.getRows();
		for(int i=0;i<list.size();i++){
			TamsRegister register = (TamsRegister)list.get(i);
			String remark = "";
			//按照是否操作了立案来处理
			if(register!=null && register.getStartDate()!=null && register.getRegisterDate()==null){
				long day=DateUtil.days(register.getStartDate(),new Date());
				long yu = Global.COMMON_REGISTER-day;
				if(yu>=0){
					remark+="余"+yu+"天";
				}else{
					remark+="<span style='color:red'>超期"+(-yu)+"天</span>";
				}
			}else if(register!=null&&register.getStartDate()!=null&&register.getRegisterDate()!=null){
				long day=DateUtil.days(register.getStartDate(),register.getRegisterDate());
				long yu = day-Global.COMMON_REGISTER;
				if(yu<=0){
					remark+="正常";
				}else{
					remark+="<span style='color:red'>超期"+yu+"天</span>";
				}
			}
			register.setRemark(remark);
		}
		return page;
	}
	
	/**
	 * 鉴定列表查询
	 * @param dgm
	 * @param caseQo
	 * @param personId
	 * @return
	 */
	public Page queryIdentify(DataGridModel dgm,CaseQo caseQo,long personId){
		Page page = identifyDao.query(dgm, caseQo,personId);
		List list = page.getRows();
		for(int i=0;i<list.size();i++){
			TamsIdentify identify = (TamsIdentify)list.get(i);
			String remark = "";
			//按照是否操作了鉴定来处理
			if(identify!=null && identify.getStartDate()!=null && identify.getEndDate()==null){
				long day=DateUtil.days(identify.getStartDate(),new Date());
				long yu = Global.COMMON_IDENTIFY-day;
				if(yu>=0){
					remark+="余"+yu+"天";
				}else{
					remark+="<span style='color:red'>超期"+(-yu)+"天</span>";
				}
			}else if(identify!=null&&identify.getStartDate()!=null&&identify.getEndDate()!=null){
				long day=DateUtil.days(identify.getStartDate(),identify.getEndDate());
				long yu = day-Global.COMMON_IDENTIFY;
				if(yu<=0){
					remark+="正常";
				}else{
					remark+="<span style='color:red'>超期"+yu+"天</span>";
				}
			}
			identify.setRemark(remark);
		}
		return page;
	}
	/**
	 * 认定列表查询
	 * @param dgm
	 * @param caseQo
	 * @param personId
	 * @return
	 */
	public Page queryConfirm(DataGridModel dgm,CaseQo caseQo,long personId){
		Page page = confirmDao.query(dgm, caseQo,personId);
		List<TamsConfirm> confirms = new ArrayList<TamsConfirm>();
		List<Object[]> obs = page.getRows();
		for(Object[] o:obs){
			TamsConfirm tcon = new TamsConfirm();
			tcon.setIdBgt(Long.parseLong(o[0].toString()));
			tcon.setCaseNo(o[1].toString());
			tcon.setDealType(Integer.parseInt(o[2].toString()));
			tcon.setConfirmDate(o[3]==null?null:DateUtil.ymdhms2Date(o[3].toString().substring(0, 20)));
			//System.out.println(o[4]==null?null:o[4].toString());
			tcon.setTamsPerson(personDao.getPersonById((Long.parseLong(o[4].toString()))));
			tcon.setConfirmContent(o[5]==null?null:o[5].toString());
			tcon.setRemark(o[6]==null?null:o[6].toString());
			tcon.setIsFinish(Integer.parseInt(o[7].toString()));
			tcon.setTamsCase(caseDao.getCaseById((Long.parseLong(o[8].toString()))));
			tcon.setStartDate(o[9]==null?null:DateUtil.ymdhms2Date(o[9].toString().substring(0, 20)));
			confirms.add(tcon);
		}
		//存放caseVo
		List<ConfirmVo> list = new ArrayList<ConfirmVo>();
		for(int i=0;i<confirms.size();i++){
			ConfirmVo vo = new ConfirmVo();
			TamsConfirm c = confirms.get(i);
			//信息写入
			vo.setCaseNo(c.getCaseNo());
			vo.setDealType(c.getDealType());
			vo.setTamsPerson(c.getTamsPerson());
			vo.setConfirmDate(c.getConfirmDate());
			
			String confirmContent = partyDao.query(c.getCaseNo());
			//将认定表中认定情况和当事人情况合并
			vo.setConfirmContent(c.getConfirmContent()==null?"":c.getConfirmContent()+"\r"+confirmContent);
			
			//根据一般还是简易判断
			int interval=0;
			if(c.getTamsCase().getDealType()==Status.CASE_DEAL_COMMON){
				interval = Global.COMMON_CONFIRM;
			}else if(c.getTamsCase().getDealType()==Status.CASE_DEAL_SIMPLE){
				interval = Global.SIMPLE_CONFIRM;
			}
			String remark = "";
			//按照是否操作了认定来处理
			if(c!=null && c.getStartDate()!=null && c.getConfirmDate()==null){
				long day=DateUtil.days(c.getStartDate(),new Date());
				long yu = interval-day;
				if(yu>=0){
					remark+="余"+yu+"天";
				}else{
					remark+="<span style='color:red'>超期"+(-yu)+"天</span>";
				}
			}else if(c!=null&&c.getStartDate()!=null&&c.getConfirmDate()!=null){
				long day=DateUtil.days(c.getStartDate(),c.getConfirmDate());
				long yu = day-interval;
				if(yu<=0){
					remark+="正常";
				}else{
					remark+="<span style='color:red'>超期"+yu+"天</span>";
				}
			}
			vo.setRemark(remark);
			
			list.add(vo);
		}
		//放入page对象中list数据
		page.setRows(list);
		return page;
	}
	
	public String queryConfirm(String caseNo){
		TamsConfirm confirm = confirmDao.getCaseConfirmByNo(caseNo);
		if(confirm==null){
			return "";
		}
		String tmp = partyDao.query(caseNo);
		//将认定表中认定情况和当事人情况合并
		String confirmContent = (confirm.getConfirmContent()==null?"":confirm.getConfirmContent()+"\r"+tmp);
		return confirmContent;
	}
	/**
	 * 调解列表查询
	 * @param dgm
	 * @param caseQo
	 * @param personId
	 * @return
	 */
	public Page queryDeal(DataGridModel dgm,CaseQo caseQo,long personId){
		Page page = dealDao.query(dgm, caseQo,personId);
		List<TamsDeal> deals = new ArrayList<TamsDeal>();
		List<Object[]> obs = page.getRows();
		for(Object[] o:obs){
			TamsDeal deal = new TamsDeal();
			deal.setIdBgt(Long.parseLong(o[0].toString()));
			deal.setCaseNo(o[1].toString());
			deal.setDealType(Integer.parseInt(o[2].toString()));
			deal.setTamsPerson(personDao.getPersonById((Long.parseLong(o[3].toString()))));
			if(o[4]!=null){
				if(o[4].toString().length()>20){
					deal.setBeginDate(DateUtil.ymdhms2Date(o[4].toString().substring(0, 20)));
				}else{
					deal.setBeginDate(DateUtil.ymdhms2Date(o[4].toString()));
				}
			}
			if(o[5]!=null){
				if(o[5].toString().length()>20){
					deal.setEndDate(DateUtil.ymdhms2Date(o[5].toString().substring(0, 20)));
				}else{
					deal.setEndDate(DateUtil.ymdhms2Date(o[5].toString()));
				}
			}
			
			deal.setDealContent(o[6]==null?null:o[6].toString());
			deal.setRemark(o[7]==null?null:o[7].toString());
			deal.setIsFinish(Integer.parseInt(o[8].toString()));
			deal.setTamsCase(caseDao.getCaseById((Long.parseLong(o[9].toString()))));
			deal.setIsSuccess(Integer.parseInt(o[10].toString()));
			deal.setIsOrder(Integer.parseInt(o[11].toString()));
			deal.setOrderContent(o[12]==null?null:o[12].toString());
			deals.add(deal);
		}
		for(int i=0;i<deals.size();i++){
			TamsDeal deal = deals.get(i);
			//根据一般还是简易判断
			int interval=0;
			if(deal.getTamsCase().getDealType()==Status.CASE_DEAL_COMMON){
				interval = Global.COMMON_DEAL;
			}else if(deal.getTamsCase().getDealType()==Status.CASE_DEAL_SIMPLE){
				interval = Global.SIMPLE_DEAL;
			}
			String remark = "";
			//按照是否操作了调解来处理
			if(deal!=null && deal.getBeginDate()!=null && deal.getEndDate()==null){
				long day=DateUtil.days(deal.getBeginDate(),new Date());
				long yu = interval-day;
				if(yu>=0){
					remark+="余"+yu+"天";
				}else{
					remark+="<span style='color:red'>超期"+(-yu)+"天</span>";
				}
			}else if(deal!=null&&deal.getBeginDate()!=null&&deal.getEndDate()!=null){
				long day=DateUtil.days(deal.getBeginDate(),deal.getEndDate());
				long yu = day-interval;
				if(yu<=0){
					remark+="正常";
				}else{
					remark+="<span style='color:red'>超期"+yu+"天</span>";
				}
			}
			deal.setRemark(remark);
		}
		page.setRows(deals);
		return page;
	}
	/**
	 * 归档列表查询
	 * @param dgm
	 * @param caseQo
	 * @param personId
	 * @return
	 */
	public Page queryRecord(DataGridModel dgm,CaseQo caseQo,long personId){
		Page page = recordDao.query(dgm, caseQo,personId);
		List<TamsRecord> records = new ArrayList<TamsRecord>();
		List<Object[]> obs = page.getRows();
		for(Object[] o:obs){
			TamsRecord re = new TamsRecord();
			re.setIdBgt(Long.parseLong(o[0].toString()));
			re.setCaseNo(o[1].toString());
			re.setDealType(Integer.parseInt(o[2].toString()));
			re.setRecordDate(o[3]==null?null:DateUtil.ymdhms2Date(o[3].toString().substring(0, 20)));
			re.setRecordContent(o[4]==null?null:o[4].toString());
			re.setTamsPerson(personDao.getPersonById((Long.parseLong(o[5].toString()))));
			re.setRemark(o[6]==null?null:o[6].toString());
			re.setIsFinish(Integer.parseInt(o[7].toString()));
			re.setTamsCase(caseDao.getCaseById((Long.parseLong(o[8].toString()))));
			re.setStartDate(o[9]==null?null:DateUtil.ymdhms2Date(o[9].toString().substring(0, 20)));
			records.add(re);
		}
		for(int i=0;i<records.size();i++){
			TamsRecord record = (TamsRecord)records.get(i);
			//根据一般还是简易判断
			int interval=0;
			if(record.getTamsCase().getDealType()==Status.CASE_DEAL_COMMON){
				interval = Global.COMMON_RECORD;
			}else if(record.getTamsCase().getDealType()==Status.CASE_DEAL_SIMPLE){
				interval = Global.SIMPLE_RECORD;
			}
			String remark = "";
			//按照是否操作了调解来处理
			if(record!=null && record.getStartDate()!=null && record.getRecordDate()==null){
				long day=DateUtil.days(record.getStartDate(),new Date());
				long yu = interval-day;
				if(yu>=0){
					remark+="余"+yu+"天";
				}else{
					remark+="<span style='color:red'>超期"+(-yu)+"天</span>";
				}
			}else if(record!=null&&record.getStartDate()!=null&&record.getRecordDate()!=null){
				long day=DateUtil.days(record.getStartDate(),record.getRecordDate());
				long yu = day-interval;
				if(yu<=0){
					remark+="正常";
				}else{
					remark+="<span style='color:red'>超期"+yu+"天</span>";
				}
			}
			record.setRemark(remark);
		}
		page.setRows(records);
		return page;
	}
	/**
	 * 添加受理案件
	 * @param tamsCase
	 */
	public void add(TamsCase tamsCase){
		caseDao.add(tamsCase);
	}
	/**
	 * 更新受理案件
	 * @param tamsCase
	 */
	public void update(TamsCase tamsCase){
		caseDao.update(tamsCase);
	}
	/**
	 * 删除受理案件
	 * @param ids
	 */
	public void delete(long[] ids){
		caseDao.delete(ids);
	}
	
	/**
	 * 对案件进行催办处理
	 * @param caseNo 案件编号
	 * @param urgeDate 催办完成日期 
	 */
	public void urge(String caseNo,Date urgeDate){
		TamsCase tcase=caseDao.getCaseByNo(caseNo);
		if(tcase.getDealType()!=Status.CASE_DEAL_COMMON && tcase.getCheckType()==Status.CHECK_TYPE_PASS)
			throw new BusinessException("只有一般处理的案件才能够给进行此操作");
		//查询一般处理 记录
		TamsCommon tamsCommon = commonDao.getCaseCommonByNo(caseNo);
		if(tamsCommon==null)
			throw new BusinessException("只有一般处理的案件才能够给进行此操作");
		//设置催办完成日期
		tamsCommon.setUrgeDate(urgeDate);
		commonDao.update(tamsCommon);
	}
	
	/**
	 * 对案件进行分类处理
	 * @param caseId 案件id
	 * @param dealType 处理类型
	 * @param checkType 是否审批
	 */
	public void fenLei(long caseId,int dealType,int checkType,String recordId){
		TamsCase tcase=caseDao.getCaseById(caseId);
		if(tcase.getTamsDeptByOutDept()==null || tcase.getTamsPersonByOutPerson()==null)
			throw new BusinessException("只有指定出警单位和出警人才能够给进行此操作");
		if(tcase.getCheckType()!=Status.CHECK_TYPE_NO&&tcase.getCheckType()!=Status.CHECK_TYPE_REJECT)
			throw new BusinessException("只有尚未提交和审批拒绝的案件才能够给进行此操作");
		tcase.setDealType(dealType);//设置处理类型
		
		if(tcase.getDealType()==Status.CASE_DEAL_NOPLACE || tcase.getDealType()==Status.CASE_DEAL_CONSLUT 
				|| tcase.getDealType()==Status.CASE_DEAL_QUICK|| tcase.getDealType()==Status.CASE_DEAL_GOAWAY){
			//将归档表中写入信息
			TamsRecord tamsRecord = new TamsRecord();
			tamsRecord.setStartDate(new Date());
			tamsRecord.setIdBgt(ID.next());
			tamsRecord.setCaseNo(tcase.getCaseNo());
			tamsRecord.setDealType(tcase.getDealType());
			tamsRecord.setTamsPerson(new TamsPerson(Long.parseLong(recordId)));
			tamsRecord.setTamsCase(tcase);
			//归档记录增加
			recordDao.add(tamsRecord);
			
			//更新案件为已分配
			tcase.setDistributeStatus(Status.CASE_DISTRIBUTE_YES);
			Date distributeDate = new Date();
//			if(distributeDate.getTime()-tcase.getAlarmDate().getTime()>24*60*60*1000){
//				tcase.setIsOutTime(1);
//			}else{
//				tcase.setIsOutTime(0);
//			}
			tcase.setDistributeDate(distributeDate);
			caseDao.update(tcase);
			
			return;
		}
		
		if(checkType==1){
			tcase.setCheckType(Status.CHECK_TYPE_WAIT);//等待领导审批
			caseDao.update(tcase);
		}else if(checkType==2){
			//不需要审批,直接设置为审批通过
			tcase.setCheckType(Status.CHECK_TYPE_PASS);
			caseDao.update(tcase);
			
			//业务逻辑处理
			if(tcase.getDealType()==Status.CASE_DEAL_SIMPLE){
				//简易处理程序
				TamsSimple simple=new TamsSimple();
				simple.setIdBgt(ID.next());
				simple.setCaseNo(tcase.getCaseNo());
				simple.setCreateDate(new Date());
				simple.setIsConfirm(Status.STATUS_NO);
				simple.setIsDeal(Status.STATUS_NO);
				simple.setIsRecord(Status.STATUS_NO);
				simpleDao.add(simple);//增加简易程序对象
				
				//同时执行分配操作，将案件分配给出警人
				tcase.setDistributeStatus(Status.CASE_DISTRIBUTE_YES);
				tcase.setDistributeDate(new Date());
				caseDao.update(tcase);
				
				//在认定表中写入信息，（简易案件直接进行到认定步骤）
				TamsConfirm tamsConfirm = new TamsConfirm();
				tamsConfirm.setIdBgt(ID.next());
				tamsConfirm.setStartDate(new Date());
				tamsConfirm.setTamsCase(tcase);
				tamsConfirm.setCaseNo(tcase.getCaseNo());
				tamsConfirm.setDealType(Status.CASE_DEAL_SIMPLE);
				tamsConfirm.setTamsPerson(tcase.getTamsPersonByOutPerson());
				//dao操作
				confirmDao.add(tamsConfirm);
			}else if(tcase.getDealType()==Status.CASE_DEAL_COMMON){
				//一般处理程序
				TamsCommon common=new TamsCommon();
				common.setIdBgt(ID.next());
				common.setCaseNo(tcase.getCaseNo());
				common.setIsRegister(Status.STATUS_NO);
				common.setIsIdentify(Status.STATUS_NO);
				common.setIsConfirm(Status.STATUS_NO);
				common.setIsDeal(Status.STATUS_NO);
				common.setIsClose(Status.STATUS_NO);
				common.setIsRecord(Status.STATUS_NO);
				commonDao.add(common);
			}
		}
	}
	
	/**
	 * 对案件进行审批处理
	 * @param caseId 案件编号
	 * @param check 审批同意与否
	 * @param checkRemark 审批意见
	 */
	public void check(long caseId,String check,String checkRemark){
		TamsCase tcase=caseDao.getCaseById(caseId);
		//业务逻辑校验
		if(tcase.getCheckType()==Status.CHECK_TYPE_NO)
			throw new BusinessException("案件编号为"+tcase.getCaseNo()+"的案件还没有提交，不能再进行审批操作");
		
		if(tcase.getCheckType()==Status.CHECK_TYPE_PASS)
			throw new BusinessException("案件编号为"+tcase.getCaseNo()+"的案件已审批通过，不能再进行审批操作");
		
		if("N".equals(check)){
			if(tcase.getCheckType()==Status.CHECK_TYPE_PASS)
				throw new BusinessException("案件编号为"+tcase.getCaseNo()+"的案件已审批通过，不能再进行拒绝操作");
		}
		
		if("Y".equals(check)){
			tcase.setCheckType(Status.CHECK_TYPE_PASS);
			
			//同意后,则进行业务逻辑处理
			if(tcase.getDealType()==Status.CASE_DEAL_SIMPLE){
				//简易处理程序
				TamsSimple simple=new TamsSimple();
				simple.setIdBgt(ID.next());
				simple.setCaseNo(tcase.getCaseNo());
				simple.setCreateDate(new Date());
				simple.setIsConfirm(Status.STATUS_NO);
				simple.setIsDeal(Status.STATUS_NO);
				simple.setIsRecord(Status.STATUS_NO);
				//dao操作
				simpleDao.add(simple);//增加简易程序对象
				
				//同时执行分配操作，将案件分配给出警人
				tcase.setDistributeStatus(Status.CASE_DISTRIBUTE_YES);
				tcase.setDistributeDate(new Date());
				//在认定表中写入信息，（简易案件直接进行到认定步骤）
				TamsConfirm tamsConfirm = new TamsConfirm();
				tamsConfirm.setIdBgt(ID.next());
				tamsConfirm.setStartDate(new Date());
				tamsConfirm.setCaseNo(tcase.getCaseNo());
				tamsConfirm.setDealType(Status.CASE_DEAL_SIMPLE);
				tamsConfirm.setTamsPerson(tcase.getTamsPersonByOutPerson());
				tamsConfirm.setTamsCase(tcase);
				//dao操作
				confirmDao.add(tamsConfirm);
				
			}else if(tcase.getDealType()==Status.CASE_DEAL_COMMON){
				//一般处理程序
				TamsCommon common=new TamsCommon();
				common.setIdBgt(ID.next());
				common.setCaseNo(tcase.getCaseNo());
				common.setCreateDate(new Date());
				common.setIsRegister(Status.STATUS_NO);
				common.setIsIdentify(Status.STATUS_NO);
				common.setIsConfirm(Status.STATUS_NO);
				common.setIsDeal(Status.STATUS_NO);
				common.setIsClose(Status.STATUS_NO);
				common.setIsRecord(Status.STATUS_NO);
				//dao操作
				commonDao.add(common);
			}
			/**
			else if(tcase.getDealType()==Status.CASE_DEAL_NOPLACE || tcase.getDealType()==Status.CASE_DEAL_CONSLUT || tcase.getDealType()==Status.CASE_DEAL_QUICK){
				//将归档表中写入信息
				TamsRecord tamsRecord = new TamsRecord();
				tamsRecord.setIdBgt(ID.next());
				tamsRecord.setCaseNo(tcase.getCaseNo());
				tamsRecord.setDealType(tcase.getDealType());
				//tamsRecord.setTamsPerson(new TamsPerson(personId));
				tamsRecord.setTamsCase(tcase);
				//归档记录增加
				recordDao.add(tamsRecord);
			}
			*/
		}else{
			tcase.setCheckType(Status.CHECK_TYPE_REJECT);
		}
		tcase.setCheckRemark(checkRemark);
		//dao操作
		caseDao.update(tcase);//更新案件信息
	}
	
	/**
	 * 出警人员分配案件给重案人员
	 * @param idBgt caseId
	 * @param personId 重案人员id
	 */
	public void distribute(long idBgt,long personId){
		TamsCase tcase=caseDao.getCaseById(idBgt);
		if(tcase.getCheckType()!=Status.CHECK_TYPE_PASS)
			throw new BusinessException("只能对审批通过的案件进行此操作");
		if(tcase.getDealType()!=Status.CASE_DEAL_COMMON)
			throw new BusinessException("只能对走一般程序的案件进行此操作");
		if(tcase.getDistributeStatus()==Status.CASE_DISTRIBUTE_YES){
			//throw new BusinessException("该案件已经分配！");
		}else{
			//更新案件为已分配
			tcase.setDistributeStatus(Status.CASE_DISTRIBUTE_YES);
			Date distributeDate = new Date();
			if(distributeDate.getTime()-tcase.getAlarmDate().getTime()>24*60*60*1000){
				tcase.setIsOutTime(1);
			}else{
				tcase.setIsOutTime(0);
			}
			tcase.setDistributeDate(distributeDate);
			caseDao.update(tcase);
		}	
//		if(tcase.getCheckType()!=Status.CHECK_TYPE_PASS)
//			throw new BusinessException("");
		TamsPerson tperson=personDao.getPersonById(personId);//重案人员
			
		TamsRegister register=registerDao.queryByCase(tcase.getCaseNo());
		if(register==null){
			//建立立案信息
			register=new TamsRegister();
			register.setStartDate(new Date());
			register.setIdBgt(ID.next());
			register.setCaseNo(tcase.getCaseNo());
			register.setTamsPerson(tperson);
			register.setTamsCase(tcase);
			registerDao.add(register);//保存立案信息
		}else{
			register.setTamsPerson(tperson);
			registerDao.update(register);
		}
		
	}
	
	/**
	 * 对案件进行立案处理
	 * 
	 */
	public void register(long idBgt,int sureIdentify,String registerContent){
		//进行业务逻辑判断
		TamsCase tcase=caseDao.getCaseById(idBgt);
		if(tcase.getCheckType()!=Status.CHECK_TYPE_PASS)
			throw new BusinessException("此案件还没有通过审批，请通过审批后再操作");
		if(tcase.getDealType()!=Status.CASE_DEAL_COMMON)
			throw new BusinessException("此案件不是通过一般处理的方式不能进行立案操作");
		
		TamsCommon tcommon=commonDao.getCaseCommonByNo(tcase.getCaseNo());
		if(tcommon!=null && tcommon.getIsRegister()==Status.STATUS_YES)
			throw new BusinessException("此案件已经过立案操作，不能再次执行此操作");
		
		//判断通过开始执行立案操作
		TamsRegister register=registerDao.queryByCase(tcase.getCaseNo());
		Date registerDate = new Date();
		if(registerDate.getTime()-register.getStartDate().getTime()>5*24*60*60*1000){
			register.setIsOutTime(1);
		}else{
			register.setIsOutTime(0);
		}
		register.setRegisterDate(registerDate);
		register.setRegisterContent(registerContent);
		register.setIsFinish(Status.IS_FINISH_YES);
		register.setTamsCase(tcase);
		registerDao.update(register);
		
		//需要进行检验鉴定操作
		if(sureIdentify==Status.STATUS_YES){
			TamsIdentify identify=new TamsIdentify();
			identify.setCaseNo(tcase.getCaseNo());
			identify.setStartDate(new Date());
			identify.setIdBgt(ID.next());
			identify.setTamsPerson(register.getTamsPerson());//设置重案人员
			identify.setTamsCase(tcase);
			identifyDao.add(identify);
		}else{
			TamsIdentify identify=new TamsIdentify();
			identify.setCaseNo(tcase.getCaseNo());
			identify.setStartDate(new Date());
			identify.setEndDate(new Date());
			identify.setIdBgt(ID.next());
			identify.setTamsPerson(register.getTamsPerson());//设置重案人员
			identify.setTamsCase(tcase);
			identify.setSendDate(new Date());
			identify.setIdentifyContent("不需要鉴定");
			identify.setIsFinish(Status.IS_FINISH_YES);
			identifyDao.add(identify);
			
			//同时更新common的鉴定状态
			TamsCommon tamsCommon = commonDao.getCaseCommonByNo(tcase.getCaseNo());
			tamsCommon.setIsIdentify(Status.STATUS_YES);//设置鉴定标志
			//更新一般处理中--完成鉴定
			commonDao.update(tamsCommon);
		}
		
		//新增认定信息
		TamsConfirm confirm=new TamsConfirm();
		confirm.setIdBgt(ID.next());
		confirm.setCaseNo(tcase.getCaseNo());
		confirm.setStartDate(new Date());
		confirm.setDealType(tcase.getDealType());
		confirm.setTamsPerson(register.getTamsPerson());//设置重案人员
		confirm.setTamsCase(tcase);
		confirmDao.add(confirm);
		
		//更新一般事故表
		tcommon.setSureIdentify(sureIdentify);//是否需要检验鉴定
		tcommon.setIsRegister(Status.STATUS_YES);//已完成立案操作
		commonDao.update(tcommon);
	}
	
	/**
	 * 对案件进行鉴定处理
	 * @param caseNo 案件编号
	 * @param identifyType 鉴定类型
	 * @param identifyContent 鉴定内容
	 * @param person 认定人
	 */
	public void identify(String caseNo,String identifyType,String identifyContent,TamsPerson person){
		
		//进行业务逻辑判断
		TamsCase tcase=caseDao.getCaseByNo(caseNo);
		if(tcase.getCheckType()!=Status.CHECK_TYPE_PASS)
			throw new BusinessException("此案件还没有通过审批，请通过审批后再操作");
		if(tcase.getDealType()!=Status.CASE_DEAL_COMMON)
			throw new BusinessException("此案件不是通过一般处理的方式不能进行鉴定操作");
		
		TamsCommon tcommon=commonDao.getCaseCommonByNo(tcase.getCaseNo());
		if(tcommon.getIsIdentify()==Status.STATUS_YES){
			//throw new BusinessException("此案件已经过鉴定操作，不能再次执行此操作");
		}else{
			//查询案件受理记录
			//TamsCase tamsCase = caseDao.getCaseByNo(caseNo);
			
			TamsCommon tamsCommon = commonDao.getCaseCommonByNo(caseNo);
			tamsCommon.setIsIdentify(Status.STATUS_YES);//设置鉴定标志
			//更新一般处理中--完成鉴定
			commonDao.update(tamsCommon);
		}
		
		//进行鉴定记录修改
		TamsIdentify tamsIdentify = identifyDao.getCaseIdentifByNo(caseNo);
		tamsIdentify.setCaseNo(caseNo);
		tamsIdentify.setIdentifyType(identifyType);
		tamsIdentify.setEndDate(new Date());
		tamsIdentify.setSendDate(new Date());
		tamsIdentify.setIdentifyContent(identifyContent);
		tamsIdentify.setIsFinish(Status.IS_FINISH_YES);
		tamsIdentify.setTamsCase(tcase);
		
		//更新鉴定中-类型、结束日期、送达日期、内容
		identifyDao.update(tamsIdentify);
		
//		//生成事故认定记录
//		TamsConfirm tamsConfirm = confirmDao.getCaseConfirmByNo(caseNo);
//		tamsConfirm.setIdBgt(ID.next());
//		tamsConfirm.setCaseNo(caseNo);
//		tamsConfirm.setDealType(tamsCase.getDealType());
//		tamsConfirm.setTamsPerson(person);
//		//增加到数据库
//		confirmDao.add(tamsConfirm);
		
	}
	
	/**
	 * 进行认定（实际是更新认定）  并且新增调解记录
	 * @param caseNo
	 * @param confirmContent
	 * @param personId
	 */
	public void confirm(String caseNo,String confirmContent,long personId){
		
		//进行业务逻辑判断
		TamsCase tcase=caseDao.getCaseByNo(caseNo);
		if(tcase.getCheckType()!=Status.CHECK_TYPE_PASS)
			throw new BusinessException("此案件还没有通过审批，请通过审批后再操作");
		
		//设定标志位 表示是否之前认定过
		boolean flag = false;
		
		//查询案件受理记录
		TamsCase tamsCase = caseDao.getCaseByNo(caseNo);
		//判断是否是一般案件
		if(tamsCase.getDealType()==Status.CASE_DEAL_COMMON){
			//查询一般案件处理记录
			TamsCommon tamsCommon = commonDao.getCaseCommonByNo(caseNo);
			//判断是否已经执行了认定操作
			if(tamsCommon.getIsConfirm()==Status.STATUS_YES){
				//throw new BusinessException("此案件已经过认定操作，不能再次执行此操作");
				//设定flag
				flag = true;
			}else{
				tamsCommon.setIsConfirm(Status.STATUS_YES);//设置认定标志
				//更新一般处理中--完成认定
				commonDao.update(tamsCommon);
			}
		}else if(tamsCase.getDealType()==Status.CASE_DEAL_SIMPLE){
			//查询简易案件处理记录
			TamsSimple tamsSimple = simpleDao.getCaseSimpleByNo(caseNo);
			
			//判断是否已经执行了认定操作
			if(tamsSimple.getIsConfirm()==Status.STATUS_YES){
				//throw new BusinessException("此案件已经过认定操作，不能再次执行此操作");
				//设定flag
				flag = true;
			}else{
				tamsSimple.setIsConfirm(Status.STATUS_YES);//设置认定标志
				//更新简易处理中--完成认定
				simpleDao.update(tamsSimple);
			}
		}
		
		
		TamsConfirm tamsConfirm = confirmDao.getCaseConfirmByNo(caseNo);
		//更新认定中-案件编号  认定内容   认定日期   案件类型
		tamsConfirm.setCaseNo(caseNo);
		tamsConfirm.setConfirmContent(confirmContent);
		tamsConfirm.setConfirmDate(new Date());
		tamsConfirm.setDealType(tamsCase.getDealType());
		tamsConfirm.setIsFinish(Status.IS_FINISH_YES);
		tamsConfirm.setTamsCase(tcase);
		confirmDao.update(tamsConfirm);
		
		
		if(flag){
			TamsDeal tamsDeal = dealDao.getCaseDealByNo(caseNo);
			tamsDeal.setTamsPerson(new TamsPerson(personId));
			dealDao.update(tamsDeal);
		}else{
			//将调解表中写入信息，caseNo，调解人等
			TamsDeal tamsDeal = new TamsDeal();
			tamsDeal.setIdBgt(ID.next());
			Date beginDate = new Date();
			tamsDeal.setBeginDate(beginDate);
			if(beginDate.getTime()-tamsCase.getAlarmDate().getTime()>2*24*60*60*1000){
				if(tamsCase.getDealType()==Status.CASE_DEAL_SIMPLE){
					tamsCase.setIsOutTime(1);
					caseDao.update(tamsCase);
				}
			}
			tamsDeal.setCaseNo(caseNo);
			//设置未知
			tamsDeal.setIsSuccess(-1);
			tamsDeal.setDealType(tamsCase.getDealType());
			tamsDeal.setTamsPerson(new TamsPerson(personId));
			tamsDeal.setTamsCase(tcase);
			//调解记录增加
			dealDao.add(tamsDeal);
		}
	}
	
	
	/**
	 * 进行调解（实际是更新调解）  并且新增归档记录
	 * @param caseNo
	 * @param dealContent
	 * @param personId
	 */
	public void deal(String caseNo,String dealContent,long personId,int isSuccess){
		//查询案件受理记录
		TamsCase tamsCase = caseDao.getCaseByNo(caseNo);
		if(tamsCase.getCheckType()!=Status.CHECK_TYPE_PASS)
			throw new BusinessException("此案件还没有通过审批，请通过审批后再操作");
		
		//设定标志位 表示是否之前认定过
		boolean flag = false;
		
		//判断是否是一般案件
		if(tamsCase.getDealType()==Status.CASE_DEAL_COMMON){
			//查询一般案件处理记录
			TamsCommon tamsCommon = commonDao.getCaseCommonByNo(caseNo);
			//判断是否已经执行了调解操作
			if(tamsCommon.getIsDeal()==Status.STATUS_YES){
				//throw new BusinessException("此案件已经过调解操作，不能再次执行此操作");
				//设定flag
				flag = true;
			}else{
				tamsCommon.setIsDeal(Status.STATUS_YES);//设置调解标志
				tamsCommon.setIsClose(Status.STATUS_YES);//设置结案标志
				//更新一般处理中--完成调解
				commonDao.update(tamsCommon);
			}
		}else if(tamsCase.getDealType()==Status.CASE_DEAL_SIMPLE){
			//查询简易案件处理记录
			TamsSimple tamsSimple = simpleDao.getCaseSimpleByNo(caseNo);
			
			//判断是否已经执行了调解操作
			if(tamsSimple.getIsDeal()==Status.STATUS_YES){
				//throw new BusinessException("此案件已经过调解操作，不能再次执行此操作");
				//设定flag
				flag = true;
			}else{
				tamsSimple.setIsDeal(Status.STATUS_YES);//设置调解标志
				//更新简易处理中--完成调解
				simpleDao.update(tamsSimple);
			}
		}
		
		
		TamsDeal tamsDeal = dealDao.getCaseDealByNo(caseNo);
		//更新调解中-调解情况、调解完成日期、调解类型
		tamsDeal.setDealContent(dealContent);
		tamsDeal.setEndDate(new Date());
		tamsDeal.setIsSuccess(isSuccess);
		tamsDeal.setDealType(tamsCase.getDealType());
		tamsDeal.setIsFinish(Status.IS_FINISH_YES);
		tamsDeal.setTamsCase(tamsCase);
		confirmDao.update(tamsDeal);
		
		if(flag){
			TamsRecord tamsRecord = recordDao.getCaseRecordByNo(caseNo);
			tamsRecord.setTamsPerson(new TamsPerson(personId));
			recordDao.update(tamsRecord);
		}else{
			//自动结案  将结案表中写入信息
			TamsClose tamsClose = new TamsClose();
			tamsClose.setIdBgt(ID.next());
			tamsClose.setCloseDate(new Date());
			tamsClose.setCaseNo(caseNo);
			tamsClose.setCloseContent("结案");
			tamsClose.setIsFinish(Status.IS_FINISH_YES);
			tamsClose.setTamsPerson(tamsDeal.getTamsPerson());
			tamsClose.setTamsCase(tamsCase);
			//结案记录增加
			closeDao.add(tamsClose);
			
			//将归档表中写入信息
			TamsRecord tamsRecord = new TamsRecord();
			tamsRecord.setStartDate(new Date());
			tamsRecord.setIdBgt(ID.next());
			tamsRecord.setCaseNo(caseNo);
			tamsRecord.setDealType(tamsCase.getDealType());
			tamsRecord.setTamsPerson(new TamsPerson(personId));
			tamsRecord.setTamsCase(tamsCase);
			//归档记录增加
			recordDao.add(tamsRecord);
		}
	}
	
	
	/**
	 * 进行归档（实际是更新归档） 
	 * @param caseNo
	 * @param recordContent
	 */
	public void record(String caseNo,String recordContent){
		//查询案件受理记录
		TamsCase tamsCase = caseDao.getCaseByNo(caseNo);
		if(tamsCase.getDealType()==Status.CASE_DEAL_COMMON|| tamsCase.getDealType()==Status.CASE_DEAL_SIMPLE){
			if(tamsCase.getCheckType()!=Status.CHECK_TYPE_PASS)
				throw new BusinessException("此案件还没有通过审批，请通过审批后再操作");
		}
		
		//判断是否是一般案件
		if(tamsCase.getDealType()==Status.CASE_DEAL_COMMON){
			//查询一般案件处理记录
			TamsCommon tamsCommon = commonDao.getCaseCommonByNo(caseNo);
			//判断是否已经执行了认定操作
			if(tamsCommon.getIsRecord()==Status.STATUS_YES){
				//throw new BusinessException("此案件已经过归档操作，不能再次执行此操作");
			}else{
				tamsCommon.setIsRecord(Status.STATUS_YES);//设置归档标志
				//更新一般处理中--完成归档
				commonDao.update(tamsCommon);
			} 
		}else if(tamsCase.getDealType()==Status.CASE_DEAL_SIMPLE){
			//查询简易案件处理记录
			TamsSimple tamsSimple = simpleDao.getCaseSimpleByNo(caseNo);
			
			//判断是否已经执行了认定操作
			if(tamsSimple.getIsRecord()==Status.STATUS_YES){
				//throw new BusinessException("此案件已经过归档操作，不能再次执行此操作");
			}else{
				tamsSimple.setIsRecord(Status.STATUS_YES);//设置归档标志
				//更新简易处理中--完成归档
				simpleDao.update(tamsSimple);
			}
		}
		
		
		TamsRecord tamsRecord = recordDao.getCaseRecordByNo(caseNo);
		//更新归档中-归档内容  案件编号   归档日期   处理类型
		tamsRecord.setRecordContent(recordContent);
		tamsRecord.setRecordDate(new Date());
		tamsRecord.setDealType(tamsCase.getDealType());
		tamsRecord.setIsFinish(Status.IS_FINISH_YES);
		tamsRecord.setTamsCase(tamsCase);
		recordDao.update(tamsRecord);
	}
	
	public CaseAllDetail queryAllDetail(String caseNo){
		CaseAllDetail cad = new CaseAllDetail();
		TamsCase tamsCase = caseDao.getCaseByNo(caseNo);
		cad.setCaseNo(tamsCase.getCaseNo());
		cad.setAlarmAddress(tamsCase.getAlarmAddress());
		cad.setAlarmDate(tamsCase.getAlarmDate());
		cad.setAlarmName(tamsCase.getAlarmName());
		cad.setAlarmPhone(tamsCase.getAlarmPhone());
		cad.setAlarmType(tamsCase.getAlarmType());
		cad.setAlarmWay(tamsCase.getAlarmWay());
		cad.setCaseAddress(tamsCase.getCaseAddress());
		cad.setRemark(tamsCase.getRemark());
		cad.setCaseContent(tamsCase.getCaseContent());
		cad.setCheckType(tamsCase.getCheckType());
		cad.setDealType(tamsCase.getDealType());
		cad.setSex(tamsCase.getSex());
		cad.setOutPersonName(tamsCase.getTamsPersonByOutPerson().getPersonName());
		cad.setOutDeptName(tamsCase.getTamsDeptByOutDept().getDeptName());
		cad.setInPersonName(tamsCase.getTamsPersonByInPerson().getPersonName());
		cad.setInDeptName(tamsCase.getTamsDeptByInDept().getDeptName());
		cad.setIsDanger(tamsCase.getIsDanger());
		
		//组装鉴定内容
		String identifyContent = identifyDao.queryIdentifyContent(caseNo);
		cad.setIdentifyContent(identifyContent);
		//组装认定表和当时人表中的数据
		String confirmContent = confirmDao.queryDealContent(caseNo);
		cad.setConfirmContent(confirmContent+partyDao.query(caseNo));
		//组装调解内容
		String dealContent = dealDao.queryDealContent(caseNo);
		cad.setDealContent(dealContent);
		//组装催办日期
		TamsCommon c = commonDao.getCaseCommonByNo(caseNo);
		Date urgeDate = c==null?null:c.getUrgeDate();
		cad.setUrgeDate(urgeDate);
		return cad;
	}
	
	/**
	 * 调解申请
	 * @param caseNo
	 * @param dealContent
	 */
	public void dealOrder(String caseNo,String dealContent){
		TamsDeal  deal = dealDao.getCaseDealByNo(caseNo);
		if(deal==null){
			throw new BusinessException("该案件还没有完成认定，不能进行调解预约！");
		}
		TamsRecord record = recordDao.getCaseRecordByNo(caseNo);
		if(record!=null){
			throw new BusinessException("该案件已经完成归档，不能进行调解预约！");
		}
		deal.setDealContent(dealContent);
		deal.setIsOrder(1);
		dealDao.update(deal);
	}
}
